package sweetvsti;

import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author uimac
 */
public class TestWave {

    private int sampleSize = 0;
    private static final double MIDI_SCALER = 1.0D / 127.0D;
    private static final int NUM_PROGRAMS = 16;
    private static final int NUM_OUTPUTS = 2;
    private VSTiProgram[] programs = new VSTiProgram[NUM_PROGRAMS];
    private int currentProgram;
    private float freq;
    private float volume = 0.33f;
    private int channelPrograms[] = new int[NUM_PROGRAMS];
    private int currentNote = 60;;
    private int currentVelocity;
    private int currentDelta;
    private boolean noteIsOn;
    // 再生中WAVEファイルの位置。sampleSizeで数えて何個目かを表す
    private double phase;
    private double basePhase = 1.0;
    private AudioFormat format = null;
    // waveデータ
    private static byte[][] data = null;
    // 128鍵盤の周波数テーブル
    private double freq_table[] = new double[0x7F];
    private byte[] buffer = new byte[128000];

    public void reset() {
        Settings.instance().loadAudioFiles();
    }


    public TestWave() {
        reset();
//        this.format = stream.getFormat();
//        this.sampleSize = stream.available();//128000;
//        this.basePhase = sampleSize / format.getSampleRate();
//        this.data = new byte[1][sampleSize];
    }

    public void noteOn(int note) {
        // waveディレクトリが変更された
        if (Settings.instance().isIsWaveBaseDirChanged()) {
            this.reset();
            Settings.instance().setIsWaveBaseDirChanged(false);
        }
        
        File[][] files = Settings.instance().getFiles();
        int charactor = Settings.instance().getCharactor();
        int key = note;
        if (key < 0) {
            key = 0;
        }
        if (key > files.length) {
            key = files.length - 1;
        }
        File file = files[key][charactor];
        play(file);
    }
    
    public void play(File file) {
        if (file == null) return;

        AudioInputStream stream = null;
        try {
            if (file.exists()) {
                stream = AudioSystem.getAudioInputStream(file);

                int sampleSize = stream.available();
                if (buffer.length != sampleSize) {
                    buffer = new byte[sampleSize];
                }

                AudioFormat format = stream.getFormat();
                DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
                SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
                line.open(format);
                line.start();

                for (int readBytes = stream.read(buffer, 0, sampleSize);
                        readBytes >= 0;
                        readBytes = stream.read(buffer, 0, sampleSize)) {
                    if (readBytes >= 0) {
                        line.write(buffer, 0, readBytes);
                    }
                }

                line.drain();
                line.close();

                stream.close();
                stream = null;
            }
        } catch (Exception ex) {
            if (stream != null) {
                try {
                    stream.close();
                    stream = null;
                } catch (Exception exx) {
                }
            }
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                    stream = null;
                } catch (Exception exx) {
                }
            }
        }
    }
}
